unit clientutils;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, fpstructs, lNetComponents;

function RequestData( terminalID: integer; connection: TLTCPComponent; datatype: TfpDatatype; data: Pchar=nil): Boolean;
function UpdateData( terminalID: integer; connection: TLTCPComponent; datatype: TfpDatatype; data: Pointer=nil; datasize: integer=0): Boolean;

implementation

function RequestData( terminalID: integer; connection: TLTCPComponent; datatype: TfpDatatype; data: Pchar=nil
  ): Boolean;
var
  Datastream: TMemoryStream;
  header: tfpHeader;
  datalength: integer;
  datastr: string;
begin
  Result := false;
  Datastream := TMemoryStream.Create;
  try
    header.MessageType := fpmRequestConfigData;
    header.TerminalID := terminalID;
    header.datalength := 1;
    header.datatype := datatype;
    Datastream.Write(header, SizeOf(header));
    if data<>nil then
       begin
         datastr := data;
         datalength := Length(datastr);
         Datastream.Write(datalength, SizeOf(datalength));
         Datastream.Write(datastr[1], datalength);
         writeln('definitely requesting data ', datastr, ' with length ', datalength);
       end;
    Datastream.Position := 0;
    Result := connection.Send(Datastream.Memory^, Datastream.Size)=Datastream.Size;
  finally
    Datastream.Free;
  end;
end;

function UpdateData(terminalID: integer; connection: TLTCPComponent;
  datatype: TfpDatatype; data: Pointer; datasize: integer=0): Boolean;
var
  Datastream: TMemoryStream;
  header: tfpHeader;
  datalength: integer;
  datastr: string;
begin
  Result := false;
  Datastream := TMemoryStream.Create;
  try
    header.MessageType := fpmSubmitConfigData;
    header.TerminalID := terminalID;
    header.datatype := fpdProductRef;
    header.datalength := datasize;
    Datastream.Write(header, SizeOf(header));
    if data<>nil then
       begin
         Datastream.Write(data^, datasize);
       end;
    Datastream.Position := 0;
    Result := connection.Send(Datastream.Memory^, Datastream.Size)=Datastream.Size;
  finally
    Datastream.Free;
  end;
end;

end.

